package com.kuang.filter;

import com.kuang.util.Constant;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author: Zhang Dongwei
 * Date: 2022/4/17 19:46
 */
public class SysFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        // 这里是ServletRequest取不到Session,我们使用的是HttpServletRequest可以取Session,所以我们需要转一下
        HttpServletRequest request=(HttpServletRequest) req;
        HttpServletResponse response=(HttpServletResponse) resp;
        Object user_session = request.getSession().getAttribute(Constant.USER_SESSION);
        if(user_session==null){
            response.sendRedirect("/error.jsp");
        }
        chain.doFilter(req,resp);
    }
    public void destroy() {

    }
}
